﻿using AutoMapper;
using MediatR;
using S.Organization.API.Application.Command.CottageCommand;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;

namespace S.Organization.API.Application.CommandHandler.CottageCommandHandler
{
    public class DelAllCottageCommandHandler : IRequestHandler<DelAllCottageCommand, int>
    {
        private readonly IBaseRepository<CottageSetting> _repository;
        private readonly IBaseRepository<FieldSetting> r;
        private readonly IMapper mapper;

        public DelAllCottageCommandHandler(IBaseRepository<CottageSetting> repository, IBaseRepository<FieldSetting> r, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
            this.r = r;
        }

        public async Task<int> Handle(DelAllCottageCommand request, CancellationToken cancellationToken)
        {
            string[] id = request.ids.ToString().Split(',');
            var list = await r.GetAllAsync();

         
            foreach (var item in id)
            {
                foreach (var item1 in list)
                {
                    if (item == item1.CottageId.ToString())
                    {
                        await r.DeleteAsync(item1.FieldId);
                    }
                }
                await  _repository.DeleteAsync(int.Parse(item));
            }
            return 1;
        }
    }
}
